package postService

import (
	"errors"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/suite"
	"notabug.org/ropemann/imabon/src/common"
	"notabug.org/ropemann/imabon/src/entities/post"
)

type PostServiceTestSuite struct {
	suite.Suite
	author   string
	body     string
	image    string
	threadId string
	board    string
	postId   string
}

func (s *PostServiceTestSuite) SetupSuite() {
	common.LoadConfig("../../../.env")
	s.author = "test_author"
	s.body = "test_body"
	s.image = "test_image"
	s.threadId = "625b8273c84324aea98ce004"
	s.board = "ani"

	CreatePost = func(board, threadId string, post *post.Post) (int64, error) {
		return 122, nil
	}

	DeletePost = func(board, threadId, postId string) (int64, error) {
		return 3, nil
	}
}

func (s *PostServiceTestSuite) TestCreate() {
	post := post.NewPost(s.author, s.body, s.image)
	res, err := Create(s.board, s.threadId, *post)
	assert.Nil(s.T(), err)
	assert.NotNil(s.T(), res)
}

func (s *PostServiceTestSuite) TestCreateNoBody() {
	var eq int64 = 0
	post := post.NewPost(s.author, "", s.image)
	res, err := Create(s.board, s.threadId, *post)
	assert.NotNil(s.T(), err)
	assert.Equal(s.T(), res, eq)
}

func (s *PostServiceTestSuite) TestCreateFail() {
	var eq int64 = 0
	CreatePost = func(board, threadId string, post *post.Post) (int64, error) {
		return 0, errors.New("test error")
	}

	post := post.NewPost(s.author, s.body, s.image)
	res, err := Create(s.board, s.threadId, *post)
	assert.NotNil(s.T(), err)
	assert.Equal(s.T(), res, eq)
}

func (s *PostServiceTestSuite) TestDelete() {
	res, err := Delete(s.board, s.threadId, s.postId)
	assert.Nil(s.T(), err)
	assert.Greater(s.T(), int(res), 0)
}

func TestPostServiceTestSuite(t *testing.T) {
	suite.Run(t, new(PostServiceTestSuite))
}
